<div class="header_menu">
    <div class="container">
        <div class="header-nav clearfix">
            <?php echo $this->partial('shop/layout/headerMenu'); ?>
        </div>
    </div>
</div>
</div>

<div class="container">
    <div class="xm-plain-box">
        <div class="box-hd">
            <h3 class="title"><?php echo $this->translate('会员注册'); ?></h3>
        </div>

        <?php if(isset($this->registerStatus) && !$this->registerStatus) { ?>
        <div class="box-bd">
            <div class="row">
                <p><strong><?php echo $this->registerCloseMessage; ?></strong></p>
            </div>
        </div>
        <?php } else { ?>
            <?php
            $form = $this->form;
            $form->prepare();
            $form->setAttributes(['class' => 'form-horizontal']);

            $form->get('userName')->setAttributes([
                'class' => 'form-control',
                'placeholder' => $this->translate('请输入登录名称')
            ]);
            $form->get('userPassword')->setAttributes([
                'class' => 'form-control',
                'placeholder' => $this->translate('请输入登录密码')
            ]);
            $form->get('userComPassword')->setAttributes([
                'class' => 'form-control',
                'placeholder' => $this->translate('请输入确认密码')
            ]);

            $registerEmailState = $this->shopHelper()->customer('register_email_state', 'customer');
            $registerPhoneState = $this->shopHelper()->customer('register_phone_state', 'customer');
            $phoneCaptchaState  = $this->shopHelper()->customer('register_phone_captcha', 'customer');
            ?>
        <div class="box-bd">
            <div class="row">
                <?php echo $this->form()->openTag($form); ?>
                <?php echo $this->formElement($form->get('shopCsrf')); ?>
                <?php echo $this->formElementErrors($form->get('shopCsrf'), ['class'=>'error-message']); ?>
                        <div class="control-group">
                            <label for="input01" class="control-label"><?php echo $this->translate('会员名称'); ?><span class="must_add_value">*</span>：</label>
                            <div class="controls">
                                <?php echo $this->formElement($form->get('userName')); ?>
                                <?php echo $this->formElementErrors($form->get('userName'), ['class'=>'error-message']); ?>
                            </div>
                        </div>
                        <div class="control-group">
                            <label for="input01" class="control-label"><?php echo $this->translate('会员密码'); ?><span class="must_add_value">*</span>：</label>
                            <div class="controls">
                                <?php echo $this->formElement($form->get('userPassword')); ?>
                                <?php echo $this->formElementErrors($form->get('userPassword'), ['class'=>'error-message']); ?>
                            </div>
                        </div>
                        <div class="control-group">
                            <label for="input01" class="control-label"><?php echo $this->translate('确认密码'); ?><span class="must_add_value">*</span>：</label>
                            <div class="controls">
                                <?php echo $this->formElement($form->get('userComPassword')); ?>
                                <?php echo $this->formElementErrors($form->get('userComPassword'), ['class'=>'error-message']); ?>
                            </div>
                        </div>
                        <?php if ($registerEmailState == 1) {
                            $form->get('userEmail')->setAttributes([
                                'class' => 'form-control',
                                'placeholder' => $this->translate('请输入电子邮箱地址')
                            ]);
                            ?>
                            <div class="control-group">
                                <label for="input01" class="control-label"><?php echo $this->translate('电子邮箱'); ?><span class="must_add_value">*</span>：</label>
                                <div class="controls">
                                    <?php echo $this->formElement($form->get('userEmail')); ?>
                                    <?php echo $this->formElementErrors($form->get('userEmail'), ['class'=>'error-message']); ?>
                                </div>
                            </div>
                        <?php } ?>
                        <?php if ($registerPhoneState == 1) { ?>
                            <div class="control-group">
                                <label for="input01" class="control-label"><?php echo $this->translate('手机号码'); ?><span class="must_add_value">*</span>：</label>
                                <div class="controls">
                                    <?php
                                    $form->get('userPhone')->setAttributes([
                                        'class' => 'form-control',
                                        'placeholder' => $this->translate('联系电话')
                                    ]);
                                    if(count($this->phoneArea) == 1) { ?>
                                        <?php echo $this->formElement($form->get('areaCode')); ?>
                                        <?php echo $this->formElementErrors($form->get('areaCode'), ['class'=>'error-message']); ?>
                                        <?php echo $this->formElement($form->get('userPhone')); ?>
                                        <?php echo $this->formElementErrors($form->get('userPhone'), ['class'=>'error-message']); ?>
                                    <?php } else {
                                        $form->get('areaCode')->setAttributes([
                                            'style' => 'width:150px'
                                        ]);
                                        ?>
                                        <?php echo $this->formElement($form->get('areaCode')); ?>
                                        <?php echo $this->formElementErrors($form->get('areaCode'), ['class'=>'error-message']); ?>

                                        <?php echo $this->formElement($form->get('userPhone')); ?>
                                        <?php echo $this->formElementErrors($form->get('userPhone'), ['class'=>'error-message']); ?>
                                    <?php } ?>
                                </div>
                            </div>
                            <?php if($phoneCaptchaState == 1) { ?>
                                <div class="control-group">
                                    <label for="input01" class="control-label"><?php echo $this->translate('手机验证码'); ?><span class="must_add_value">*</span>：</label>
                                    <div class="controls">
                                        <?php echo $this->formElement($form->get('phoneCaptcha')->setAttributes(['class' => 'form-control', 'placeholder' => $this->translate('手机验证码')])); ?>
                                        <button id="get_phone_captcha" type="button" class="button button-code btn btn-info "><?php echo $this->translate('获取验证码'); ?></button>
                                        <?php echo $this->formElementErrors($form->get('phoneCaptcha'), ['class'=>'error-message']); ?>
                                    </div>
                                </div>
                            <?php } ?>
                        <?php } ?>

                        <?php if($this->shopHelper()->shop('user_register_captcha', 'captcha') == 1) { ?>
                            <div class="control-group">
                                <label for="input01" class="control-label"><?php echo $this->translate('验证码'); ?><span class="must_add_value">*</span>：</label>
                                <div class="controls">
                                    <?php echo $this->formElement($form->get('userRegisterCaptcha')->setAttributes(['class' => 'form-control', 'placeholder' => $this->translate('输入上面的验证码')])); ?>
                                    <?php echo $this->formElementErrors($form->get('userRegisterCaptcha'), ['class'=>'error-message']); ?>
                                </div>
                            </div>
                        <?php } ?>
                        <div class="control-group">
                            <label for="input01" class="control-label"><?php echo $this->translate('注册协议'); ?><span class="must_add_value">*</span>：</label>
                            <div class="controls">
                                <label class="checkbox">
                                    <?php echo $this->formElement($form->get('agreement')); ?>
                                    <?php echo $this->translate('我已阅读并同意'); ?> <a href="javascript:;" onclick="showAgreement();" class="agree"> <b><?php echo $this->translate('用户注册协议'); ?></b></a>
                                </label>
                                <?php echo $this->formElementErrors($form->get('agreement'), ['class'=>'error-message']); ?>
                            </div>
                        </div>
                        <div class="control-group">
                            <div class="controls">
                                <?php echo $this->formElement($form->get('httpReferer')); ?>
                                <button class="btn btn-primary" type="submit"><?php echo $this->translate('提交注册用户'); ?></button>
                            </div>
                        </div>
                        <?php if (!empty($this->enableLogin)) { ?>
                            <div class="control-group">
                                <label for="input01" class="control-label"><?php echo $this->translate('其他登录'); ?>：</label>
                                <div class="controls">
                                    <?php foreach ($this->enableLogin AS $loginValue) { ?>
                                       <a href="<?php echo $this->url('shop-user', ['action' => 'otherLogin'], ['query' => ['loginType' => $loginValue]]); ?>" style="margin-right: 8px;"><img src="<?php echo $this->basePath('template/pc/default/img/'.$loginValue.'.png'); ?>" style="width: 36px;"></a>
                                    <?php } ?>
                                </div>
                            </div>
                        <?php } ?>
                <?php echo $this->form()->closeTag(); ?>
            </div>
        </div>
    </div>
</div>
<script>
    $(document).ready(function() {
        $("#<?php echo $form->getName(); ?>").validate({
            rules: {
                userName: {
                    required: true
                },
                <?php if ($registerEmailState == 1) { ?>
                userEmail: {
                    required: true,
                    email: true
                },
                <?php } ?>
                <?php if ($registerPhoneState == 1) { ?>
                userPhone: {
                    required: true
                },
                <?php if ($phoneCaptchaState == 1) { ?>
                phoneCaptcha: {
                    required: true
                },
                <?php } ?>
                <?php } ?>
                userPassword: {
                    required: true,
                    minlength: 6
                },
                userComPassword: {
                    required: true,
                    minlength: 6,
                    equalTo: '#userPassword'
                },
                agreement: {
                    required: true
                }
            },
            messages: {
                userName: {
                    required: "<?php echo $this->translate('请输入登录名称!'); ?>"
                },
                <?php if ($registerEmailState == 1) { ?>
                userEmail: {
                    required: "<?php echo $this->translate('请输入电子邮箱地址!'); ?>",
                    email: "<?php echo $this->translate('请输入有效的电子邮箱地址!'); ?>"
                },
                <?php } ?>
                <?php if ($registerPhoneState == 1) { ?>
                userPhone: {
                    required: "<?php echo $this->translate('请输入联系电话!'); ?>"
                },
                <?php if ($phoneCaptchaState == 1) { ?>
                phoneCaptcha: {
                    required: "<?php echo $this->translate('请输入手机验证码!'); ?>"
                },
                <?php } ?>
                <?php } ?>
                userPassword: {
                    required: "<?php echo $this->translate('请输入登录密码!'); ?>",
                    minlength: "<?php echo $this->translate('密码最少6位!'); ?>"
                },
                userComPassword: {
                    required: "<?php echo $this->translate('请输入确认密码!'); ?>",
                    minlength: "<?php echo $this->translate('确认密码最少6位!'); ?>",
                    equalTo: "<?php echo $this->translate('两次输入的密码不一致!'); ?>"
                },
                agreement: {
                    required: "<?php echo $this->translate('同意注册协议后，才能注册!'); ?>"
                }
            }
        });
    });

    function showAgreement() {
        layer.open({
            type: 2,
            title: '<?php echo $this->translate('注册协议'); ?>',
            shadeClose: true,
            shade: 0.8,
            area: ['60%', '90%'],
            content: '<?php echo $this->url('shop-user', ['action'=>'registerAgreement']); ?>'
        });
    }

    <?php if ($registerPhoneState == 1 && $phoneCaptchaState == 1) { ?>
    var oBtn = document.getElementById('get_phone_captcha');
    var flag = true;

    oBtn.addEventListener("click", function () {
        $.ajax({
            type: 'POST',
            url : "<?php echo $this->url('shop/default', ['action'=>'sendPhoneCaptcha']); ?>",
            data: {"phoneNumber": $('#userPhone').val(), "areaCode": $('#areaCode').val()},
            success : function (msg) {
                if(typeof msg == "undefined" || msg == null || msg == '') {
                    startSend(<?php echo $this->shopHelper()->getPhoneCaptchaSecond(); ?>);
                } else {
                    layer.msg(msg);
                }
            }
        });
    });

    function startSend(expiredTime) {
        var time = expiredTime;
        oBtn.classList.add('disabled');
        $('#userPhone').attr('readonly', true);
        if (flag) {
            flag = false;
            var timer = setInterval(() => {
                time--;
                oBtn.innerText = '已发送 ' + time + ' 秒';
                if (time === 0) {
                    clearInterval(timer);
                    oBtn.innerText = '重新获取';
                    oBtn.classList.remove('disabled');
                    $('#userPhone').attr('readonly', false);
                    flag = true;
                }
            }, 1000)
        }
    }
    <?php if(((int)$this->shopHelper()->getDbshopSessionData('expiredTime') - time()) > 0) { ?>
    startSend(<?php echo (int)$this->shopHelper()->getDbshopSessionData('expiredTime') - time(); ?>);
    <?php } ?>
    <?php } ?>
</script>
<?php } ?>